from koubei.action import count_pages, collect_kid, scrape_koubei, dump_koubei
# from utils import Brand, Manufactor, Series

from .exception import CountPageError

def pipeline(ssid, target_path):
    ## Pipeline操作，共有三个步骤
    ## 1. 获取koubei总页数
    ## 2. 获取所有的koubei id
    ## 3. 获取所有的koubei信息，并保存到本地

    try:
        page_count = count_pages(ssid)
    except Exception as e:
        raise CountPageError(e)
    kids = collect_kid(ssid, page_count)
    dump_koubei(kids, target_path, ssid)

def search(brand_name, brands):
    # 根据输入的品牌名字，在品牌列表中查找对应的品牌
    for brand in brands:
        if brand_name == brand.brand_name:
            return brand
        
def get_series(bms_list):
    series = []
    for bms in bms_list:
        for s in bms['series']:
            if s['series_price']: # 没有价格信息，非有效车型；
                series.append(s['series_id'])
    return series
